<?php

/**
 * @author Murat Cem YALIN <muratcem@gmail.com>
 * @copyright 2014 muratcem.com - walkerPHP.com
 * @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3
 * @version 1.0.1
 */

use \core\Config\Config;
use \core\Session\Session;

include_once 'helpers.php';
include_once 'defines.php';

walker2php_AutoLoader::register();

Session::start();

$module 	    = (isset($_GET['m']) && !empty($_GET['m'])) ? trim($_GET['m']) : Config::get('siteSettings.mainModule');
$controllerFile = 'pub/controllers/'.$module.'.php';

if (file_exists($controllerFile)){
    include_once $controllerFile;
    if (class_exists($module, FALSE)){
        $controller = new $module;
    } else {
        include_once 'pub/404NotFound.html';
    }
} else {
    include_once 'pub/404NotFound.html';
}



class walker2php_AutoLoader{
    /**
     * registering the autloader with SPL
     *
     * @return bool
     */
    public static function register(){
        if (function_exists('__autoload')){
            spl_autoload_register('__autoload');
        }
        if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
            return spl_autoload_register(array('walker2php_AutoLoader', 'load'), TRUE, TRUE);
        } else {
            return spl_autoload_register(array('walker2php_AutoLoader', 'load'));
        }
    }

    /**
     * Loads the given class given in $classname
     *
     * @param $className
     * @return void
     */
    public static function load($className){
        $className = ltrim($className, '\\');
        $fileName  = '';
        if ($lastNsPos = strrpos($className, '\\')) {
            $namespace = substr($className, 0, $lastNsPos);
            $className = substr($className, $lastNsPos + 1);
            $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
        }
        $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

        require $fileName;
    }
}